/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.eb.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.eb.entity.OrderBargin;
import com.kaaction.modules.eb.service.OrderBarginService;

/**
 * 砍价Controller
 * @author zengbiao
 * @version 2017-05-04
 */
@Controller
@RequestMapping(value = "${adminPath}/eb/orderBargin")
public class OrderBarginController extends BaseController {

	@Autowired
	private OrderBarginService orderBarginService;
	
	@ModelAttribute
	public OrderBargin get(@RequestParam(required=false) String id) {
		OrderBargin entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = orderBarginService.get(id);
		}
		if (entity == null){
			entity = new OrderBargin();
		}
		return entity;
	}
	
	@RequiresPermissions("eb:orderBargin:view")
	@RequestMapping(value = {"list", ""})
	public String list(OrderBargin orderBargin, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OrderBargin> page = orderBarginService.findPage(new Page<OrderBargin>(request, response), orderBargin); 
		model.addAttribute("page", page);
		return "modules/eb/orderBarginList";
	}

	@RequiresPermissions("eb:orderBargin:view")
	@RequestMapping(value = "form")
	public String form(OrderBargin orderBargin, Model model) {
		model.addAttribute("orderBargin", orderBargin);
		return "modules/eb/orderBarginForm";
	}

	@RequiresPermissions("eb:orderBargin:edit")
	@RequestMapping(value = "save")
	public String save(OrderBargin orderBargin, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, orderBargin)){
			return form(orderBargin, model);
		}
		orderBarginService.save(orderBargin);
		addMessage(redirectAttributes, "保存砍价成功");
		return "redirect:"+Global.getAdminPath()+"/eb/orderBargin/?repage";
	}
	
	@RequiresPermissions("eb:orderBargin:edit")
	@RequestMapping(value = "delete")
	public String delete(OrderBargin orderBargin, RedirectAttributes redirectAttributes) {
		orderBarginService.delete(orderBargin);
		addMessage(redirectAttributes, "删除砍价成功");
		return "redirect:"+Global.getAdminPath()+"/eb/orderBargin/?repage";
	}

}